package com.kk.clone.ShallowClone;

import cn.hutool.core.clone.CloneSupport;

/**
 * 浅克隆,嵌套对象不会被克隆，克隆出来的嵌套对象地址不变
 * <p>
 * 2.CloneSupport类实现了Object的clone方法，所以不用重写，缺点：单继承
 *
 * @author 王志强
 * @date 2022-02-21
 */
public class Dog extends CloneSupport<Dog> {

    private String name = "汪汪";
    private final int age = 2;
    private Dog dog = null;


    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.dog = new Dog();

        //这里先客隆，再改变狗中狗的名字值，所以克隆的狗中狗的名字不会变，为 汪汪
        Dog cloneDog = dog.clone();
        dog.dog.name = "旺财";

        //浅克隆导致的问题：克隆过后改变原嵌套对象的值，克隆后的嵌套对象的值也会跟着变
        System.out.println(dog + " - " + dog.name + " - " + dog.age + " - " + dog.dog);

        //这里克隆狗中狗的名字应该为 汪汪，而打印出来的是 旺财！
        System.out.println(cloneDog + " - " + cloneDog.name + " - " + cloneDog.age + " - " + cloneDog.dog + " - " + cloneDog.dog.name);
    }
}
